package com.hducms.cms.action.front;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hducms.cms.entity.front.ChannelM;
import com.hducms.cms.entity.front.ChannelResponse;
import com.hducms.cms.entity.main.Channel;
import com.hducms.cms.entity.main.CmsUser;
import com.hducms.cms.service.ChannelService;
import com.hducms.cms.web.CmsUtils;
import com.hducms.common.util.ClientUtils;

@Controller
public class ChannelActM {
//	private static final Logger log = LoggerFactory.getLogger(ChannelActM.class);
	@RequestMapping(value="/channellist.m")
	public void getChannel(HttpServletRequest request,
			HttpServletResponse response,String publickey)  {
		CmsUser user = CmsUtils.getUser(request);
		int groupId  = user.getGroupId();
		Integer[] channelIds = manager.findViewChannelIdsByGroupId(groupId);
		Set<Channel> viewChannels = new HashSet<Channel>();
		for (int temp : channelIds) {
			Channel tempChannel = manager.findById(temp);
			if (null != tempChannel && tempChannel.getParentid() != null) {
				viewChannels.add(tempChannel);
			}
		}
		List<ChannelM> channellistM = new ArrayList<ChannelM>();
		for (Channel channel : viewChannels) {
			ChannelM temp =  new ChannelM();
			temp.setId(channel.getId());
			temp.setName(channel.getName());
			temp.setPriority(channel.getPriority());
			channellistM.add(temp);
		}
		ClientUtils.resultResponse(response, channellistM, publickey);
//		ClientUtils.resultResponse(response, new ChannelResponse(channellistM), publickey);
	}
	
	@Autowired
	private ChannelService manager;
}
